#include "structure/Frame.h"
#include "TextureMgr.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform2.hpp>
#include <glm/gtx/projection.hpp>

Frame::Frame(GLuint programHandler, GLShaderConfig shaderConfig, float size) :
	GLStructure(programHandler, shaderConfig),
	size(size) {
	column1 = new Cube(programHandler, shaderConfig);
	column2 = new Cube(programHandler, shaderConfig);
	column3 = new Cube(programHandler, shaderConfig);
	column4 = new Cube(programHandler, shaderConfig);

	components.push_back(column1);
	components.push_back(column2);
	components.push_back(column3);
	components.push_back(column4);

	column1->setTextureData(TextureMgr::getInstance()->getBoomFrameDiffuseTextureData(), TextureMgr::getInstance()->getBoomFrameDirtTextureData());
	column2->setTextureData(TextureMgr::getInstance()->getBoomFrameDiffuseTextureData(), TextureMgr::getInstance()->getBoomFrameDirtTextureData());
	column3->setTextureData(TextureMgr::getInstance()->getBoomFrameDiffuseTextureData(), TextureMgr::getInstance()->getBoomFrameDirtTextureData());
	column4->setTextureData(TextureMgr::getInstance()->getBoomFrameDiffuseTextureData(), TextureMgr::getInstance()->getBoomFrameDirtTextureData());
}

Frame::~Frame() {
	components.clear();

	delete column1;
	delete column2;
	delete column3;
	delete column4;
}

void Frame::updateComponents() {
	glm::mat4 transformation = glm::translate(transformationMatrix, glm::vec3(0, 0, size / 2));
	transformation = glm::rotate(transformation, 45.0f, glm::vec3(0, 1, 0));
	transformation = glm::scale(transformation, glm::vec3(0.2, size, 0.2));
	column1->transform(transformation);

	transformation = glm::translate(transformationMatrix, glm::vec3(0, 0,  -size / 2));
	transformation = glm::rotate(transformation, 45.0f, glm::vec3(0, 1, 0));
	transformation = glm::scale(transformation, glm::vec3(0.2, size, 0.2));
	column2->transform(transformation);

	transformation = glm::translate(transformationMatrix, glm::vec3(0, size / 2, 0));
	transformation = glm::rotate(transformation, 45.0f, glm::vec3(0, 0, 1));
	transformation = glm::scale(transformation, glm::vec3(0.2, 0.2, size));
	column3->transform(transformation);

	transformation = glm::translate(transformationMatrix, glm::vec3(0, -size / 2, 0));
	transformation = glm::rotate(transformation, 45.0f, glm::vec3(0, 0, 1));
	transformation = glm::scale(transformation, glm::vec3(0.2, 0.2, size));
	column4->transform(transformation);
}

void Frame::update (glm::mat4 viewMatrix, glm::mat4 modelMatrix) {
	updateComponents();
	TextureMgr::getInstance()->loadTextures(programHandler);
	std::list<GLObject*>::iterator it = components.begin();
	for ( ; it != components.end(); it++ ) {
		(*it)->update(viewMatrix, modelMatrix);
	}
}


